package com.jinzhi.jzweb.service.impl;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.jzweb.domain.ExaminationResultsComDO;
import com.jinzhi.jzweb.domain.moduleConfig.CareStuSignUpComRealityDO;
import com.jinzhi.jzweb.pojo.ExaminationAnswerSheetComOneExlVO;
import com.jinzhi.jzweb.pojo.StuSignUpTeamVo;
import com.jinzhi.jzweb.pojo.StuVo;
import com.jinzhi.jzweb.service.ExaminationResultsComService;
import com.jinzhi.jzweb.service.moduleConfig.CareStuSignUpComRealityService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.StuSignUpComDao;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.service.StuSignUpComService;
import com.jinzhi.common.base.CoreServiceImpl;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 
 * <pre>
 * 考生报名
 * </pre>
 * <small> 2023-05-05 08:52:02 | lance</small>
 */
@Service
public class StuSignUpComServiceImpl extends CoreServiceImpl<StuSignUpComDao, StuSignUpComDO> implements StuSignUpComService {


    @Override
    public List<StuSignUpTeamVo> sumTeamStuSignExam(String name) {
        return baseMapper.sumTeamStuSignExam( name);
    }

    @Override
    public List<StuSignUpTeamVo> sumOneStuSignExam(String name) {
        return baseMapper.sumOneStuSignExam(name);
    }

    private final ExaminationResultsComService examinationResultsComService;

    private final CareStuSignUpComRealityService careStuSignUpComRealityService;

    @Override
    public Integer MaxNum(Long cbatchId) {
        return baseMapper.MaxNum(cbatchId);
    }


    public StuSignUpComServiceImpl(ExaminationResultsComService examinationResultsComService, CareStuSignUpComRealityService careStuSignUpComRealityService) {
        this.examinationResultsComService = examinationResultsComService;
        this.careStuSignUpComRealityService = careStuSignUpComRealityService;
    }

    @Override
    public Page<StuSignUpComDO> getStuGignUpCom(Integer curr,Integer pageSize) {
        Page<StuSignUpComDO> page = new Page<>(
                Optional.of(curr).orElse(1),
                Optional.of(pageSize).orElse(10));
        //查询全部考生信息
        List<StuSignUpComDO> sign = selectList(new EntityWrapper<StuSignUpComDO>());
        //获取考生三个赛场得分
        //根据学生id查询成绩
        ExaminationResultsComDO examinationResultsComDO = new ExaminationResultsComDO();
        sign.forEach(a ->{
            examinationResultsComDO.setStuSignId(a.getId());
            ExaminationResultsComDO rcd = examinationResultsComService.selectOne(examinationResultsComDO);
            if(rcd!=null) {

                Double a111 = ((rcd.getScoingb1() + rcd.getScoingb2() + rcd.getScoingb3())/3);
                a111 = a111 + (rcd.getTotalScore() *  0.3); //理论成绩
                BigDecimal a1 = new BigDecimal(a111);
                double a11 = a1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                rcd.setScoreA(String.valueOf(a11));

                BigDecimal b1 = new BigDecimal(((rcd.getScore1() + rcd.getScore2() + rcd.getScore3())/3));
                double b11 = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                rcd.setScoreB(String.valueOf(b11));

                BigDecimal c1 = new BigDecimal(((rcd.getScoingc1() + rcd.getScoingc2() + rcd.getScoingc3())/3));
                double c11 = c1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                rcd.setScoreC(String.valueOf(c11));

                a.setScore(Double.valueOf(rcd.getScoreA())+Double.valueOf(rcd.getScoreB())+Double.valueOf(rcd.getScoreC()));
                a.setExaminationResultsComDO(rcd);
            }
        });
        sign = sign.stream().sorted(Comparator.comparing(StuSignUpComDO::getScore,Comparator.nullsFirst(Double::compareTo)).reversed()).collect(Collectors.toList());
        page.setRecords(forDes(sign));
        return page;
    }

    public List<StuSignUpComDO> forDes(List<StuSignUpComDO> list1){
        if(list1.size()==0){
            return new ArrayList<>();
        }
        for (int i = 0; i < list1.size(); i++) {
            list1.get(i).setDes(i+1);
        }
        return list1;
    }



    @Override
    public void stuGignUpCom(HttpServletResponse response) {
        List<StuVo> stuVos = new ArrayList<>();
        //查询全部考生信息
        List<StuSignUpComDO> sign = selectList(new EntityWrapper<StuSignUpComDO>().eq("decrypt",1));
        //获取考生三个赛场得分
        //根据学生id查询成绩
        ExaminationResultsComDO examinationResultsComDO = new ExaminationResultsComDO();
        sign.forEach(a ->{
            StuVo stuVo = new StuVo();
            examinationResultsComDO.setStuSignId(a.getId());
            ExaminationResultsComDO rcd = examinationResultsComService.selectOne(examinationResultsComDO);
            if(rcd!=null) {

                Double a111 = ((rcd.getScoingb1() + rcd.getScoingb2() + rcd.getScoingb3())/3);
                a111 = a111 + (rcd.getTotalScore() *  0.3); //理论成绩
                BigDecimal a1 = new BigDecimal(a111);
                double a11 = a1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                rcd.setScoreA(String.valueOf(a11));

                BigDecimal b1 = new BigDecimal(((rcd.getScore1() + rcd.getScore2() + rcd.getScore3())/3));
                double b11 = b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                rcd.setScoreB(String.valueOf(b11));

                BigDecimal c1 = new BigDecimal(((rcd.getScoingc1() + rcd.getScoingc2() + rcd.getScoingc3())/3));
                double c11 = c1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                rcd.setScoreC(String.valueOf(c11));

                a.setScore(Double.valueOf(rcd.getScoreA())+Double.valueOf(rcd.getScoreB())+Double.valueOf(rcd.getScoreC()));
                a.setExaminationResultsComDO(rcd);

                stuVo.setScoreA(String.valueOf(a11));
                stuVo.setScoreB(String.valueOf(b11));
                stuVo.setScoreC(String.valueOf(c11));
            }
            stuVo.setNum2(a.getNum2());
            stuVo.setRealityUm2(a.getCandidateNumber());
            stuVo.setName(a.getName());
            stuVo.setIdNumber(a.getIdNumber());
            stuVo.setUnitName(a.getUnitName());
            stuVo.setTeacher(a.getTeacher());
            stuVo.setComNums(a.getComNums());
            stuVos.add(stuVo);

        });
        sign.stream().sorted(Comparator.comparing(StuSignUpComDO::getScore,Comparator.nullsFirst(Double::compareTo)).reversed()).collect(Collectors.toList());
        //导出操作
        EasyPoiUtil.exportExcel(stuVos, "选手解密导出", "选手解密导出", StuVo.class, "选手解密导出", response);
    }

    /**
     * 生成二次加密好
     */
    @Override
    public void touchSign() {
        Wrapper<StuSignUpComDO> wrapper1 = new EntityWrapper<StuSignUpComDO>().eq("competition_type", 1);
        List<StuSignUpComDO> signList1 = this.selectList(wrapper1);
        if(null!=signList1 && !signList1.isEmpty()){
            int i = 1;
            for (StuSignUpComDO stuSignUpComDO : signList1) {
                Random random = new Random();
                String cipher = String.valueOf(random.nextInt(900000) + 100000);
                stuSignUpComDO.setRandomCipher(cipher);
                String num2 = "";
                if(i<10){
                    num2 = "A0"+i;
                }else{
                    num2 = "A"+i;
                }
                stuSignUpComDO.setNum2(num2);
                i++;

                //同步到实际表
                CareStuSignUpComRealityDO careStuSignUpComRealityDO = new CareStuSignUpComRealityDO();
                careStuSignUpComRealityDO.setNum2(num2);
                careStuSignUpComRealityDO.setRandomCipher(cipher);
                careStuSignUpComRealityService.update(careStuSignUpComRealityDO,new EntityWrapper<CareStuSignUpComRealityDO>().eq("unit_name",stuSignUpComDO.getUnitName()).eq("name",stuSignUpComDO.getName()));
            }
            this.updateBatchById(signList1);
        }

        Wrapper<StuSignUpComDO> wrapper2 = new EntityWrapper<StuSignUpComDO>().eq("competition_type", 2);
        List<StuSignUpComDO> signList2 = this.selectList(wrapper2);
        if(null!=signList2 && !signList2.isEmpty()){
            int i = 1;
            for (StuSignUpComDO stuSignUpComDO : signList2) {
                Random random = new Random();
                String cipher = String.valueOf(random.nextInt(900000) + 100000);
                stuSignUpComDO.setRandomCipher(cipher);
                String num2 = "";
                if(i<10){
                    num2 = "A0"+i;
                }else{
                    num2 = "A"+i;
                }
                stuSignUpComDO.setNum2(num2);
                i++;
                //同步到实际表
                CareStuSignUpComRealityDO careStuSignUpComRealityDO = new CareStuSignUpComRealityDO();
                careStuSignUpComRealityDO.setNum2(num2);
                careStuSignUpComRealityDO.setRandomCipher(cipher);
                careStuSignUpComRealityService.update(careStuSignUpComRealityDO,new EntityWrapper<CareStuSignUpComRealityDO>().eq("unit_name",stuSignUpComDO.getUnitName()).eq("name",stuSignUpComDO.getName()));
            }
            this.updateBatchById(signList2);
        }

    }

//    public List<StuVo> forDess(List<StuVo> list1){
//        if(list1.size()==0){
//            return new ArrayList<>();
//        }
//        for (int i = 0; i < list1.size(); i++) {
//            list1.get(i).setDes(i+1);
//        }
//        return list1;
//    }
}
